package com.gec.auction.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Configuration
public class DateConfig implements WebMvcConfigurer {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new StringToDateConverter());
    }

    private class StringToDateConverter implements Converter<String, Date> {

        private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        @Override
        public Date convert(String source) {
            try {
                return format.parse(source);
            } catch (ParseException e) {
                e.printStackTrace();
                return null;
            }
        }
    }
}